#__Author:Administrator
#__Date:2019/6/25
import os
import time
import pickle
#BASE_DIR = os.path.dirname(os.path.dirname(__file__))
#sys.path.append(BASE_DIR)
from src import models
from src import identifier
from config import setting

class BaseModel:
    # def read(self, role):
    #     """
    #     从数据文件里加载所有对应角色对象信息
    #     :param role:
    #     :return: ret 对象列表
    #     """
    #     ret = []
    #     db_file = os.listdir(self.db_path)
    #     for item in db_file:
    #         ret.append(pickle.load(self, open(os.path.join(db_file, item), 'rb')))
    #     return ret

    def save(self):
        """
        保存对象信息
        :return:
        """
        nid = str(self.nid)
        file_path = os.path.join(self.db_path, nid)
        if not os.path.exists(self.db_path):
            os.makedirs(self.db_path)
        pickle.dump(self, open(file_path, 'wb'))


class Admin(BaseModel):
    db_path = setting.ADMIN_DB
    def __init__(self, user, pwd):
        """
        创建管理员对象 :param user: :param pwd:
        """
        self.user = user
        self.pwd = pwd
        self.nid = identifier.AdminNid(Admin.db_path).uuid
        self.time = time.strftime('%Y-%m-%d %H:%M:%S')
        print(self.nid)

    @staticmethod
    def login(user, pwd):
        for file in os.listdir(Admin.db_path):
            obj = pickle.load(open(os.path.join(Admin.db_path, file), 'rb'))
            if obj.user == user and obj.pwd == pwd:
                return obj
        return None


class School(BaseModel):
    db_path= setting.SCHOOL_DB
    def __init__(self, name, city):
        self.nid = identifier.StudentNid(School.db_path).uuid
        self.schoolName = name
        self.schoolCity = city
        self.students_number = 0

    def __str__(self):
        return self.schoolName

    def income_students_number(self):
        self.students_number += 1

    @staticmethod
    def get_all_list():
        """
        列出所有学校
        :return:
        """
        obj = []
        for file in os.listdir(School.db_path):
            obj.append(pickle.load(open(os.path.join(School.db_path, file), 'rb')))
        return obj

class Classes(BaseModel):
    db_path = setting.CLASSES_DB
    def __init__(self, name, school_id):
        self.nid = identifier.ClassesNid(Classes.db_path).uuid
        self.classesName = name
        self.schoolID = school_id
        self.students_list = []
        self.teacher_list = []
        self.students_number = 0
    def __str__(self):
        return ("学校:%s 班级：%s" % (self.schoolID.schoolName, self.classesName))

    def create_teacher(self, teacher_id):
        self.teacher_list.append(teacher_id)

    def create_students(self, students_id):
        """
        为班级分配学生
        :param students_id:
        :return:
        """
        self.students_number += 1
        self.students_list.append(students_id)

    @property
    def show_classes_info(self):
        """
        查看班级信息
        :return:
        """
        print("\033[34m ------班级信息----- \033[0m")
        print("学校：%s 班级：%s 人数：%s" % (self.schoolID.schoolName, self.classesName, self.students_number))
        print("\033[34m ------讲师信息----- \033[0m")
        for i in self.teacher_list:
            print("讲师:%s 任课：%s" % (i.teacherID.teacherName, i.courseID.courseName))
        print("\033[34m ------学生信息----- \033[0m")
        for i in self.students_list:
            print("学生名:%s 专业:%s 成绩：%d" % (i.user, i.courseID.courseName, i.show_score))

    @staticmethod
    def get_all_list():
        obj = []
        for file in os.listdir(Classes.db_path):
            obj.append(pickle.load(open(os.path.join(Classes.db_path, file), 'rb')))
        return obj

class Teacher(BaseModel):
    db_path = setting.TEACHER_DB
    def __init__(self, name, pwd, level):
        self.teacherName = name
        self.teacherPwd = pwd
        self.teacherLevel = level
        self.nid = identifier.TeacherNid(Teacher.db_path).uuid
        self.income = 0

    def __str__(self):
        return self.teacherName

    def income(self, var):
        self.income += var

    @staticmethod
    def get_all_list():
        """
        列出所有讲师
        :return:
        """
        obj = []
        for file in os.listdir(Teacher.db_path):
            obj.append(pickle.load(open(os.path.join(Teacher.db_path, file), 'rb')))
        return obj

    @staticmethod
    def login(user, pwd):
        for file in os.listdir(Teacher.db_path):
            obj = pickle.load(open(os.path.join(Teacher.db_path, file), 'rb'))
            if obj.user == user and obj.pwd == pwd:
                return obj
        return None



class Student(BaseModel):
    db_path = setting.STUDENT_DB
    def __init__(self):
        """
        创建学生对象 :param user: :param pwd:
        """
        self.nid = identifier.StudentNid(Student.db_path).uuid
        self.score = 0
        self.classesId = None
        self.course_list = []

    @staticmethod
    def login(user, pwd):
        for file in os.listdir(Student.db_path):
            obj = pickle.load(open(os.path.join(Student.db_path, file), 'rb'))
            if obj.user == user and obj.pwd == pwd:
                return obj
        return None

    def register(self, user, pwd):
        """
        学生账户注册
        :param user:
        :param pwd:
        :return:
        """
        self.user = user
        self.pwd = pwd
        for file in os.listdir(Student.db_path):
            obj = pickle.load(open(os.path.join(Student.db_path, file), 'rb'))
            if obj.user == user:
                return False
            else:
                return True

    def choice_course(self, course_id):
        self.courseID = course_id
        self.course_list.append(course_id)

  #  def show_course(self):
  #      return self.course_list

    def __str__(self):
        return self.user

    def pay(self):
        print("成功支付%s" % self.courseID.coursePrice)
        self.courseID.schoolId.income_students_number()

    @property
    def show_course(self):
        #查看已报名的课程
        return self.course_list
    @property
    #查看成绩
    def show_score(self):
        #查看分数
        return self.score

    @show_score.setter
    #修改分数
    def show_score(self, var):
        self.score = var

    @property
    def allot_classes(self):
        return  self.classesId.classesName

    @allot_classes.setter
    def allot_classes(self, classes_id):
        self.classesId = classes_id

    @property
    def get_course_teacher(self):
        return self.classesId.teacher_list


    @staticmethod
    def get_all_list():
        obj = []
        for file in os.listdir(Student.db_path):
            obj.append(pickle.load(open(os.path.join(Student.db_path, file), 'rb')))
        return obj

class Course(BaseModel):
    db_path = setting.COURSE_DB
    def __init__(self, name, price, period, school_id):
        """
        :param name: 课程名
        :param price:  课程价格
        :param period: 课程周期
        :param school_id: 关联学校id,
        """
        self.courseName = name
        self.coursePrice = price
        self.coursePeriod = period
        self.schoolId = school_id
        self.nid = identifier.CourseNid(Course.db_path).uuid
    def __str__(self):
        return ("学校名:%s \n 课程名:%s \n 价格:%s \n 周期:%s" % (self.schoolId, self.courseName, self.coursePrice, self.coursePeriod))

    @staticmethod
    def get_all_list():
        """
        列出所有课程
        :return:
        """
        obj = []
        for file in os.listdir(Course.db_path):
            obj.append(pickle.load(open(os.path.join(Course.db_path, file), 'rb')))
        return obj

class Course_to_Teacher(BaseModel):
    db_path = setting.COURSE_TO_TEACHER_DB
    def __init__(self, course_id, teacher_id):
        self.nid = identifier.Course_to_TeacherNid(Course_to_Teacher.db_path).uuid
        self.courseID = course_id
        self.teacherID = teacher_id

    def __str__(self):
       return ("讲师: %s 课程：%s" % (self.teacherID.teacherName, self.courseID.courseName))

    def get_teacher_name(self):
        return self.teacherID.teacherName

    def get_course_name(self):
        return self.courseID.courseName

    @staticmethod
    def get_all_list():
        """
        列出所有课程
        :return:
        """
        obj = []
        for file in os.listdir(Course_to_Teacher.db_path):
            obj.append(pickle.load(open(os.path.join(Course_to_Teacher.db_path, file), 'rb')))
        return obj

